<?php
// lay ten cua tac vu hoac cong viec
function _getname($arr_task,$v_spec_task)
{
	$n = sizeof($arr_task);
	for($i = 0 ; $i < $n ; $i++)
		if($arr_task[$i][0] == $v_spec_task)
			return $arr_task[$i][1];
}
// hien thi link tuong ung voi cac cong viec o menu ben trai
// $_isa_current_module_code : ma chuc nang nguoi dung dang thao tac hien hanh
// $user_id : id cua NSD dang nhap
// $recordtype : loai ho so NSD muon thao tac
function spec_work_show_left_menu_links($_isa_current_module_code, $user_id, $recordtype_id)
{
	global $v_name_worktype_spec,$recordtype_id_list,$v_staff_id_list,$arr_all_spec_worktype,$v_count_spec_worktype,$arr_all_spec_worktype,$v_count_spec_worktype,$v_name_spec_worktype;
	$str = RECORD_LABEL;
	if($_isa_current_module_code == "RECEIVE_RESULT" || $_isa_current_module_code == "HANDLE" || $_isa_current_module_code == "APPROVING")
	{			
			$str .= " ".$v_name_spec_worktype;		
	}
	return $str;
}
// hien thi cac radio tuong ung de cap nhat ho so trong chuc nang ket qua thu ly, phe duyet
// $_isa_current_module_code : ma chuc nang nguoi dung dang thao tac hien hanh
// $user_id : id cua NSD dang nhap
// $recordtype : loai ho so NSD muon thao tac
function spec_work_show_update_radio($_isa_current_module_code,	$user_id, $recordtype)
{

}
// hien thi cac radio tuong ung de cap nhat ho so trong chuc nang cap nhat tien do
// $user_id : id cua NSD dang nhap
// $recordtype : loai thu tuc muon thao tac
function spec_work_show_update_progress($user_id,$recordtype)
{
	global $v_current_style_name,$v_worktype_id,$v_disable,$v_checked,$arr_all_spec_worktype,$v_count_spec_worktype,$v_name_spec_worktype;;
	
	for($i = 0 ; $i < $v_count_spec_worktype; $i++)
		{
			$arr_all_staff_id = explode(",",$arr_all_spec_worktype[$i]['C_STAFF_ID_LIST']);
			if(in_array($user_id,$arr_all_staff_id))	// neu can bo co trong danh sach cau hinh
				$str = "<tr class=". $v_current_style_name.">
							<td ><input type='radio' name='opt_worktype_id' readonly='true' message = 'CONST_WORKTYPE_MESSAGE' value='".$v_worktype_id."' ".$v_disable.$v_checked ."onClick='document.forms(0).hdn_work.value = this.value ; document.getElementById('worktype').innerHTML=document.forms(0).".$v_worktype_id."value;></td>
							<td colspan='10'>".$v_name_spec_worktype."</td>
							<td><input type='hidden' name='".$v_worktype_id."' value=".$v_worktype_name."></td>
						</tr>";
		}
	return $str;
}
// ham ngung thoi gian
// set cho cot C_APPOINTED_DATE = ""
function clone_time()
{
	// ham tuong ung trong cot C_SPEC_TASK_START	
	// tra ve ngay ngung
	return "";
}
// tiep tuc tinh thoi gian
// $date_current : ngay tiep tuc thuc hien
// $date_stop : ngay dung
function release_time($date_stop,$date_current,$v_received_date,$v_process_date)
{
	// ham tuong ung trong cot C_SPEC_TASK_START	
	//global $v_received_date, $v_appointed_date,$v_process_date;	
	//echo $v_process_date;
	//$v_count_date_recent = dateDiff($v_received_date,$v_appointed_date); // lay ngay hen tra tru cho ngay dung = so ngay con lai	
	$v_stop_date = dateDiff($v_received_date,$date_stop);	// tinh tu ngay tiep nhan de ngay dung lai	
	$v_total_recent = $v_process_date - $v_stop_date;	
	//$v_continue_date = $date_current;
	$arr_date = explode("/",$date_current);	
	$v_continue_date = get_appoint_date($date_current,$v_total_recent,$arr_date[2]);// tinh lai ngay hen tra, tru cac ngay thu 7, chu nhat va ngay le.
	
	// tra ve ngay hen tra tinh tu ngay tiep tuc tinh	
	return $v_continue_date ;
}

function create_sql_string_for_update_record_work($v_type_spec_work, $record_id, $v_staff_id, $v_work_date) {
	global $ado_conn;
// lay ham tuong ung clone_time hay release_time	
	$sql = "Exec Onegate_GetSpecTaskStart ";
	$sql = $sql . "'".$v_type_spec_work."'";
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$arr_spec_task_start = $ado_conn->GetAll($sql);
	//var_dump($arr_spec_task_start);
	//echo $sql;
	$date_process_spec = "";
	// lay ngay thuc hien cong viec dac biet trong tien do
	$sql = "Exec Onegate_GetAllWorkTypeCloneTime ";
	$sql = $sql . "'".$record_id."'";
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$arr_all_process_work = $ado_conn->GetAll($sql); 
	// lay ngay thuc hien ngung 	
	for($i = 0 ; $i < sizeof($arr_all_process_work) ; $i++)
	{
		$v_clone_date = $arr_all_process_work[$i]['C_DATE'];	// ngay thuc hien trong bang tien do
		$v_process_date = $arr_all_process_work[$i]['C_PROCESS_DATE'];		// so ngay thuc hien ho so
		$v_appointed_date = $arr_all_process_work[$i]['C_APPOINTED_DATE'];
		$v_received_date = $arr_all_process_work[$i]['C_RECEIVED_DATE'];		
		$v_current_status_end = $arr_spec_task_start[0]['C_CURRENT_STATUS_END'];
		$v_detail_status_end = $arr_spec_task_start[0]['C_DETAIL_STATUS_END'];
		// lay danh sach tac vu dac biet
		$v_spec_task_list = $arr_spec_task_start[0]['C_SPEC_TASK_START'];
		$arr_task_id = explode(",",$v_spec_task_list);		// mang tac vu dac biet		
		for($j = 0 ; $j < sizeof($arr_task_id); $j++)		// duyet qua tat ca cac tac vu dac biet
		{			
			if($arr_task_id[$j] == "RELEASE_TIME")	// tiep tuc tinh thoi gian sau khi dung	
			{				
				// goi ham release_time, ngay tiep tuc, ngay hen tra		
				//echo $v_clone_date;
				$date_process_spec = release_time($v_clone_date,$v_work_date,$v_received_date,$v_process_date);	// tra ve ngay hen tra			
				$date_process_spec = _ddmmyyyy_to_yyyymmdd($date_process_spec);	
				//echo $date_process_spec ;
			}
			if($arr_task_id[$j] == "CLONE_TIME")
			{			
				// goi ham clone_time, ngay tiep nhan, ngay hen tra
				$date_process_spec = clone_time(); // tra ve ngay ngung de thuc hien cong viec dac biet
				// khi dung thoi gian lai thi phai luu ngay hen tra o dau do, de sau nay tiep tuc tinh thoi gian thi lay lai de tinh so ngay con lai			
			}
			/*else
			{
				echo '3';
				$date_process_spec = $v_appointed_date;// khong cap nhat vao ngay hen tra
			}*/
			$rs = true;
			if($arr_task_id[$j] == "SEND_CO_HANDLE_DEP") 
			{
				//query danh sach can bo da tung thu ly ho so
				$sql = " select max(c_date) c_date, c_value from T_ONEGATE_ADDING_INFORMATION " .
					   " where FK_RECORD=" . $record_id . " and C_KEY = 'HANDLER' and C_DELETE = 0 " .
					   " group by c_value order by c_date desc";				
				$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
				$rs = $ado_conn->GetAll($sql); 
				$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
				//end query danh sach can bo da tung thu ly ho so
				$handler_co_handle = '';				
				if ((is_null($v_error) or $v_error=="") && $rs)
				{
					$current_unit_id = _get_item_attr_by_id($_SESSION['arr_all_staff'], $v_staff_id, 'unit_id');
					for ($i=0; $i<sizeof($rs); $i++) 
					{
						$handler_unit_id = _get_item_attr_by_id($_SESSION['arr_all_staff'], $rs[$i]['c_value'], 'unit_id');
						//echo $handler_unit_id.'aaaa';echo $rs[$i]['c_value'];
						if ($handler_unit_id != $current_unit_id && $handler_unit_id != '') 
						{
							$handler_co_handle = $rs[$i]['c_value'];
							//cap nhat phong ban dang thu ly ho so
							$sql = "update T_ONEGATE_ADDING_INFORMATION set C_VALUE='". $handler_unit_id ."' where C_KEY = 'CURRENT_UNIT' and FK_RECORD=".$record_id;
							$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
							$rs_update_info = $ado_conn->GetRow($sql); 
							$v_error_update_info = _replace_bad_char(trim($rs['RET_ERROR']));
							//end cap nhat
							//danh dau tat ca can bo thu ly ho so sau can nay bi xoa
							//$sql = "update T_ONEGATE_ADDING_INFORMATION set C_DELETE=1 where datediff(ms,'" . $rs[$i]['c_date'] ."', C_DATE) >= 0 and FK_RECORD=".$record_id;
							//$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
							//$rs_update_info = $ado_conn->GetRow($sql); 
							//$v_error_update_info = _replace_bad_char(trim($rs['RET_ERROR']));
							//end danh dau
							break;
						}
					}
				} 
				else 
				{
					exit;
				}
			}
			if($arr_task_id[$j] == "DELETE_HANDLER") 
			{
				//query danh sach can bo da tung thu ly ho so
				$sql = " select max(c_date) c_date, c_value from T_ONEGATE_ADDING_INFORMATION " .
					   " where FK_RECORD=" . $record_id . " and C_KEY = 'HANDLER' and C_DELETE = 0 " .
					   " group by c_value order by c_date desc";				
				
				$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
				$rs = $ado_conn->GetAll($sql); 
				$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
				//end query danh sach can bo da tung thu ly ho so
				// cap nhat id cua can bo thu ly (xoa id cua can bo thu ly)
				$sql = "update T_ONEGATE_RECORD set C_CURRENT_STATUS = '".$v_current_status_end."', C_DETAIL_STATUS = ".$v_detail_status_end.",
					   C_APPOINTED_DATE = '".$date_process_spec."', FK_HANDLER = NULL 
					where PK_RECORD = ".$record_id;
				$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
				$rs_ = $ado_conn->GetRow($sql);		
				
				$handler_co_handle = '';
				if ((is_null($v_error) or $v_error=="") && $rs)
				{
					$current_unit_id = _get_item_attr_by_id($_SESSION['arr_all_staff'], $v_staff_id, 'unit_id');
					for ($i=0; $i<sizeof($rs); $i++) 
					{
						$handler_unit_id = _get_item_attr_by_id($_SESSION['arr_all_staff'], $rs[$i]['c_value'], 'unit_id');
						//echo $handler_unit_id.'aaaa';echo $rs[$i]['c_value'];
						if ($handler_unit_id != $current_unit_id && $handler_unit_id != '') 
						{
							//cap nhat phong ban dang thu ly ho so
							$sql = "update T_ONEGATE_ADDING_INFORMATION set C_VALUE='". $handler_unit_id ."' where C_KEY = 'CURRENT_UNIT' and FK_RECORD=".$record_id;							
							$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
							$rs_update_info = $ado_conn->GetRow($sql); 
							$v_error_update_info = _replace_bad_char(trim($rs['RET_ERROR']));
							
							
							//echo $sql;
							//end cap nhat
							//danh dau tat ca can bo thu ly ho so sau can nay bi xoa
							//$sql = "update T_ONEGATE_ADDING_INFORMATION set C_DELETE=1 where datediff(ms,'" . $rs[$i]['c_date'] ."', C_DATE) >= 0 and FK_RECORD=".$record_id;
							//$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
							//$rs_update_info = $ado_conn->GetRow($sql); 
							//$v_error_update_info = _replace_bad_char(trim($rs['RET_ERROR']));
							//end danh dau
							break;
						}
					}
				} 
				else 
				{
					exit;
				}
			}
			$return_spec_task_array_element = explode("__", $arr_task_id[$j]);
			if ($return_spec_task_array_element[0] == "APPROVE_LEADER_RETURN") 
			{
				//query danh sach can bo da tung thu ly ho so
				$sql = " select max(c_date) c_date, c_value from T_ONEGATE_ADDING_INFORMATION " .
					   " where FK_RECORD=" . $record_id . " and C_KEY = 'HANDLER' and C_DELETE = 0 " .
					   " group by c_value order by c_date desc";
				$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
				$rs = $ado_conn->GetAll($sql); 
				$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
				//end query danh sach can bo da tung thu ly ho so
				$handler_co_handle = '';
				if ((is_null($v_error) or $v_error=="") && $rs)
				{
					$current_unit_id = $return_spec_task_array_element[1];
					for ($i=0; $i<sizeof($rs); $i++) 
					{
						$handler_unit_id = _get_item_attr_by_id($_SESSION['arr_all_staff'], $rs[$i]['c_value'], 'unit_id');
						if ($handler_unit_id == $current_unit_id) {
							$handler_co_handle = $rs[$i]['c_value'];
							//cap nhat phong ban dang thu ly ho so
							$sql = "update T_ONEGATE_ADDING_INFORMATION set C_VALUE='". $handler_unit_id ."' where C_KEY = 'CURRENT_UNIT' and FK_RECORD=".$record_id;
							$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
							$rs_update_info = $ado_conn->GetRow($sql); 
							$v_error_update_info = _replace_bad_char(trim($rs['RET_ERROR']));
							//end cap nhat
							//danh dau tat ca can bo thu ly ho so sau can nay bi xoa
							//$sql = "update T_ONEGATE_ADDING_INFORMATION set C_DELETE=1 where datediff(ms,'" . $rs[$i]['c_date'] ."', C_DATE) >= 0 and FK_RECORD=".$record_id;
							//$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
							//$rs_update_info = $ado_conn->GetRow($sql); 
							//$v_error_update_info = _replace_bad_char(trim($rs['RET_ERROR']));
							//end danh dau
							break;
						}
					}
				} else {
					exit;
				}
			}
		}
		//var_dump(getdate());exit;
		// update lai ho so		
		if ((is_null($v_error_update_info) or $v_error=="") && $rs) 
		{			
			$sql = "Exec Onegate_UpdateRecordSpecWork ";
			$sql = $sql . "'".$record_id."'";
			$sql = $sql . ",'".$v_type_spec_work."'";	
			$sql = $sql . ",'".$v_staff_id."'";	
			$sql = $sql . ",'"._ddmmyyyy_to_yyyymmdd(date("d/m/Y"))."'";
			$sql = $sql . ",'".$date_process_spec."'";
			if ($handler_co_handle != '') 
			{
				$sql = $sql . ",".$handler_co_handle;			
			}		
		} else 
		{
			return '';
		}
		//echo $sql;exit;
	}
	return $sql;
}

?>